const pagination = (query) => {
  let { currentPage, pageSize } = query;
  currentPage = Math.abs(Number(currentPage || 1));
  pageSize = Math.abs(Number(pageSize || 10));
  const offset = (currentPage - 1) * pageSize;
  return {
    currentPage,
    pageSize,
    offset,
  };
};

/**
 * 带分页功能的查询和计数函数
 * @param {Object} query - 查询参数对象，包含分页信息
 * @param {Function} queryFn - 执行实际查询的异步函数，接收分页条件参数
 * @returns {Object} 包含查询结果和分页信息的对象
 */
const findListPagination = async (query, queryFn) => {
  const { currentPage, pageSize, offset } = pagination(query);

  const paginationCondition = { limit: pageSize, offset, order: [['id', 'desc']] };

  const { count, rows } = await queryFn(paginationCondition);
  console.log(count, rows, 'paginationCondition');

  return {
    data: rows,
    pagination: {
      total: count,
      pageSize,
      currentPage,
    },
  };
};
module.exports = {
  pagination,
  findListPagination,
};
